const ModelLyricAuthor = require('../../../models/music/lyricAuthor')
const uuid = require('uuid');
const controlTool = require('../../tool')
const tool = require('../../../utils/commonTools')

// 删除词作家
exports.delLyricAuthor = (req, res, next) => {
    
    ModelLyricAuthor.delLyricAuthor(req.query
        , (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "删除成功",
                    status: 200,
                    affectedRows: value.affectedRows
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "删除失败",
                    status: 200,
                    error
                })
            })
        });
}
// 编辑词作家
exports.updateLyricAuthor = (req, res, next) => {
    const Data = req.body;
    const { lyric_author_id } = Data;
    if (lyric_author_id) {
        delete Data.lyric_author_id;
        ModelLyricAuthor.updateLyricAuthor(Data, {
            lyric_author_id
        }, (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "更新成功",
                    status: 200,
                    affectedRows: value.affectedRows
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "更新失败",
                    status: 200,
                    error
                })
            })
        });
    }
}

/** 添加词作家 */
exports.addLyricAuthor = (req, res, next) => {
    // const { music_name } = req.body;
    ModelLyricAuthor.addLyricAuthor({
        ...req.body,
        lyric_author_id: tool.orderCode()
    },
        (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "添加成功",
                    status: 200,
                    affectedRows: value.affectedRows
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "添加失败",
                    status: 200,
                    error
                })
            })
        });
}
/** 查找词作家 */
exports.findLyricAuthor = async (req, res, next) => {
    let LyricAuthor = await ModelLyricAuthor.findLyricAuthor(req.body);
    LyricAuthor.success((value) => {
        if (value.length) {
            res.render('success', {
                data: JSON.stringify({
                    message: "查找到词作家",
                    value: value,
                    status: 200,
                    length: value.length
                })
            })
        } else {
            res.render('fail', {
                data: JSON.stringify({
                    message: "没有找到词作家",
                    value: value,
                    length: value.length
                })
            })
        }
    });

    LyricAuthor.fail((error) => {
        res.render('fail', {
            data: JSON.stringify({
                message: "查找失败",
                error
            })
        })
    })
}
//查找所有词作家
exports.list = (req, res, next) => {
    const { isAll } = req.body;

    ModelLyricAuthor.findAll(
        (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "查找成功",
                    status: 200,
                    value: value,
                    length: value.length
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "查找失败",
                    status: 200,
                    error
                })
            })
        }, isAll);
}